﻿using Coldairarrow.Util;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration;
using NSwag.Annotations;
using System;
using System.IO;

namespace Coldairarrow.Api.Controllers.Base_Manage
{
    [Route("/Base_Manage/[controller]/[action]")]
    [OpenApiTag("上传")]
    public class UploadController : BaseApiController
    {
        readonly IConfiguration _configuration;
        public UploadController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        [HttpPost]
        //[AllowAnonymous]
        public IActionResult UploadFileByForm()
        {
            var file = Request.Form.Files[0];
            
            if (file == null)
                return JsonContent(new { status = "error" }.ToJson());

            string guid = "/Upload/" + Guid.NewGuid().ToString("N");
            string path = $"{guid}/{file.FileName}";
            string physicPath = GetAbsolutePath($"~{path}");
            string dir = Path.GetDirectoryName(physicPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
               
            }

            string url = $"{_configuration["WebRootUrl"]}{path}";
            var res = new
            {
                name = file.FileName,
                status = "done",
                uid = guid,
                thumbUrl = url,
                url = url
            };

                return JsonContent(res.ToJson());
        }

        
        [HttpPost]
        public string DeleteFile()
        {

            //string[] filename = FileNames.Split(',');

            string[] fils = null;
            foreach (string key in HttpContext.Request.Form.Keys)
            {
                fils = key.Split(',');
            }

            for (int i = 0; i < fils.Length; i++)
            {
                if (fils[i] != "," || fils[i] != "") {
                    PathHelper.DeleteFile(GetAbsolutePath($"~{fils[i]}"));
                }
                
            }

            return "删除";
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult downfile(IdInputDTO input)
        {
            //string filePath = GetAbsolutePath($"~/Upload/{input.id}");
            //var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);


            var addurl = Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{input.id}";
            var stream = System.IO.File.OpenRead(addurl);
            string fileext = Path.GetExtension(input.id);

            var provoide = new FileExtensionContentTypeProvider();
            var memi = provoide.Mappings[fileext];
            //return File(fileStream, "application/octet-stream", "学生导入模板.xlsx");
            return File(stream,memi,Path.GetFileName(addurl));

        }

        /// <summary>
        /// 下载协议合同
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult downSign(IdInputDTO input) 
        {
            string filePath = GetAbsolutePath($"~/{input.id}");
            var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);

            return File(fileStream, "application/octet-stream", "");
        }
    }
}
